<template>
  <div>
    <ht-dialog
      :title="title"
      center
      :visible="open"
      @close="close"
    >
      <ht-form
        ref="editForm"
        defaultEventKey="tenentInfo"
        :rowNum="1"
        :open="open"
        :rowData="editForm"
        :dictMap="dictMap"
        :formDiarules="rules"
        :formListOptions="formItemList"
        @isParentFn="isParentFn"
      />

      <!-- <el-form ref="editForm" :model="editForm" :rules="rules" label-width="80px">

        <el-form-item label="业务范围:" prop="tenantBusiScope">
          <el-select
            v-model="editForm.tenantBusiScope"
            multiple
            placeholder="请选择业务范围"
            class="common-select"
          >
            <el-option
              v-for="item in dictMap.busiScope"
              :key="item.value"
              :label="item.text"
              :value="Number(item.value)"
            ></el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="租户编号:" prop="tenantCode">
          <el-input
            v-model="editForm.tenantCode"
            placeholder="请输入租户编号"
            v-if="disabled"
          />
          <el-input
            v-model="editForm.tenantCode"
            placeholder="请输入租户编号"
            v-if="!disabled"
            disabled
          />
        </el-form-item>

        <el-form-item label="租户名称:" prop="tenantName">
          <el-input v-model="editForm.tenantName" placeholder="请输入租户名称" />
        </el-form-item>

        <el-form-item label="联系人:" prop="leader">
          <el-input v-model="editForm.leader" placeholder="请输入联系人" />
        </el-form-item>

        <el-form-item label="联系电话:" prop="phone">
          <el-input v-model="editForm.phone" placeholder="请输入联系电话" />
        </el-form-item>

        <el-form-item label="邮箱:" prop="email">
          <el-input v-model="editForm.email" placeholder="请输入邮箱" />
        </el-form-item>

      </el-form> -->

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{ $t('common.confirm') }} </el-button>
        <el-button @click="close">{{ $t('common.cancel') }}</el-button>
      </div>
    </ht-dialog>
  </div>
</template>

<script>
import { dialogMixin } from "@/libs/dialogMixin";
import { isPhone } from "@/utils/validate";
import {
  qryTenantById,
  addOrUpdateTenant,
  checkTenantCode,
} from "@/api/authority_app/tenant/tenantInfo";
import Vue from "vue";

export default {
  name: "EditTenant",
  mixins: [ dialogMixin ],
  data() {
    // 表单校验
    let checkTenantCodeUnique = (rule, value, callback) => {
      if (!value) {
        callback(new Error(`${this.lang('authority.tenantInfo.rules.tenantCodeEmpty' )}`));
      } else {

        checkTenantCode(this.editForm.id,this.editForm.tenantCode).then((response) => {
          var result = response.result;
          if (!result) {
            callback(new Error(`${this.lang('authority.tenantInfo.rules.tenantCodeExist' )}`));
          } else {
            callback();
          }
        });
      }
    };
    let validPhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error(`${this.lang('authority.tenantInfo.rules.phoneEmpty' )}`));
      } else if (!isPhone(value)) {
        callback(new Error(`${this.lang('authority.tenantInfo.rules.phoneIncorrect' )}`));
      } else {
        callback();
      }
    };
    return {
      id: '',
      disabled: false,
      // 表单校验
      rules: {
        tenantCode: [
          {
            required: true,
            trigger: "blur",
            validator: checkTenantCodeUnique,
          },
        ],
        tenantName: [
          { required: true, message: `${this.lang('authority.tenantInfo.rules.tenantNameEmpty' )}`, trigger: "blur" },
        ],
        leader: [
          { required: true, message: `${this.lang('authority.tenantInfo.rules.leaderEmpty' )}`, trigger: "blur" },
        ],
        phone: [{ required: true, trigger: "blur", validator: validPhone }],
        email: [
          { required: true, message: `${this.lang('authority.tenantInfo.rules.emailEmpty' )}`, trigger: "blur" },
          {
            type: "email",
            message: `${this.lang('authority.tenantInfo.rules.emailIncorrect' )}`,
            trigger: ["blur", "change"],
          },
        ],
      },
      formItemList: [
         {
          type: "dict",
          dict: "busiScope",
          prop: "tenantBusiScope",
          defaultAttr: {
            multiple: true,
            label: `${this.lang('authority.tenantInfo.form.tenantScopes' )}`,
            placeholder: `${this.lang('authority.tenantInfo.rules.tenantScopesEmpty' )}`,
          }
        },
        {
          type: "input",
          prop: "tenantCode",
          defaultAttr: {
            label: `${this.lang('authority.tenantInfo.form.tenantCode' )}`,
            placeholder: `${this.lang('authority.tenantInfo.rules.tenantCodeEmpty' )}`,
          },
        },
        {
          type: "input",
          prop: "tenantName",
          defaultAttr: {
            label: `${this.lang('authority.tenantInfo.form.tenantName' )}`,
            placeholder: `${this.lang('authority.tenantInfo.rules.tenantNameEmpty' )}`,
          },
        },
        {
          type: "input",
          prop: "leader",
          defaultAttr: {
            label: `${this.lang('authority.tenantInfo.form.leader' )}`,
            placeholder: `${this.lang('authority.tenantInfo.rules.leaderEmpty' )}`,
          },
        },
        {
          type: "input",
          prop: "phone",
          defaultAttr: {
            label: `${this.lang('authority.tenantInfo.form.phone' )}`,
            placeholder: `${this.lang('authority.tenantInfo.rules.phoneEmpty' )}`,
          },
        },
        {
          type: "input",
          prop: "email",
          defaultAttr: {
            label: `${this.lang('authority.tenantInfo.form.email' )}`,
            placeholder: `${this.lang('authority.tenantInfo.rules.emailEmpty' )}`,
          },
        },
      ],
    };
  },
  mounted() {
    this.loadDicts(['busiScope'])
  },
  methods: {
    show(id = '',disabled = false) {
      this.id = id;
      this.disabled = disabled;
      this.$set(this,'editForm',{})
      if(id) {
        qryTenantById(id).then((res) => {
          if(res.result.tenantBusiScope && res.result.tenantBusiScope.length > 0) {
            for(let item in res.result.tenantBusiScope) {
              res.result.tenantBusiScope[item] = '' + res.result.tenantBusiScope[item]
            }
          }
          this.$set(this,'editForm',res.result)
          Vue.delete(this.editForm,'createTime');
          Vue.delete(this.editForm,'updateTime');
          this.open = true;
        });
        this.title = this.lang('authority.tenantInfo.form.editTenant')
      } else {
        this.$set(this.editForm,'id',id)
        this.title = this.lang('authority.tenantInfo.form.addTenant')
        this.open = true;
      }
    },
    // 取消按钮
    close(state = false) {
      this.open = false
      // this.reset();
      this.$emit("editComplete", state);
    },
    /** 提交按钮 */
    isParentFn(formData) {
      const data = formData;
      data['id'] = this.id
      // this.$refs["editForm"].validate((valid) => {
        // if (valid) {
          Vue.delete(data,'createTime');
          Vue.delete(data,'updateTime');
          if(!this.id) {
            data['id'] = ''
          }
          addOrUpdateTenant(data).then((res) => {
            this.msgSuccess(`${this.lang('common.operateSuccess')}`);
            this.close(true);
          });
        // }
      // });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.editForm.sonData();
    },
  },
};
</script>
